//========================================================================
//  SE5 Select Colony Type - Captain Kwok's Balance Mod v125+
//========================================================================
//
// 1. Fx[Set_Colony_Types] Run through our list of colonies assign/update the colony type
// 2. Fx[Pick_Colony_Type] Pick a colony type for colonies without one
// 3. Fx[Update_Colony_Type] Potentially update the colony type to reflect facility changes
//
// Balance Mod Changes:
// --------------------
// v1.29 Changed - AI players will prioritize resupply in systems with limited planets (ie asteroid belt systems)
// v1.25 Changed - Adjustment to target resource values
//       Changed - Extra modifiers for colony space
//       Added   - Storage Compound colony type
// v1.19 Changed - Made improvements to AI colony selection in finite resource games (v1.19j)
//       Fixed   - AI players were not choosing the Intelligence Compound colony type (v1.19j)
//       Changed - Added Fx[Update_Colony_Type] to change an AI's colony type based on facilities present (v1.19j)
// v1.15 Added   - Additional modifiers for choosing resource colony types
// v1.14 Changed - The AI will no longer dedicated multiple planets to minor colony types in a system
// v1.12 Changed - Small tweak to resource value threshold modifiers for racial trait AIs
// v1.11 Changed - Slightly reduced the resource value thresholds for farming and refining colonies
//       Fixed   - Error that increased the threshold for refining colonies when low on radioactives
// v1.10 Changed - Refined colony type designations
// v1.09 Changed - AI will not designate an intel colony type if the game is not using intelligence operations
// v1.07 Changed - Slight increase to Intel Compound colony type
// v1.06 Changed - Introduced new resource boolean variables to further refine colony type selection
// v1.05 Added   - Revised planet size / breathable conditions for modifying colony type selection
//       Removed - Dropped Fx[Pick_Resource_Extraction_Type]
//       Added   - A few more specific colony type modifiers
//       Added   - Construction Yard colony type
// v1.04 Added   - Additional modifiers for determining AI Colony Types
// v1.03 Added   - Revised modifiers to consider domed or breathable colony
// v1.02 Added   - Some modifiers to skew colony types based on available facility space
//       Removed - Removed 90% condition for Resource Colony type
//       Changed - AI will only designate Intel Colonies if the tech area is available
// v1.00 Tweaked - Different percentages for research and resource colony types
// v0.99 Tweaked - Increased percentage of colonies to designate as Intel Compounds
// v0.97 Added   - Mixed-use colony types
// v0.96 Changed - Tweaked selection criteria for colony types
// v0.93 Changed - Updated comments for colony types selection
// v0.92 Changed - Tweaked Resource Colony selection
// v0.84 Added   - Resource Colony as a new colony type
// v0.83 Changed - Created new criteria for selection of colony types
//
// Balance Mod To Do List:
// -----------------------
// - None
//
// Script Function Requests:
// -------------------------
// - None

//------------------------------------------------------------------------
// Forward Declarations
//------------------------------------------------------------------------
deffunc

function Set_Colony_Types returns boolean
params
end

function Pick_Colony_Type returns string
params
  colony_id:                 long
  total_colonies:            long
end

function Update_Colony_Type returns string
params
  colony_id:                 long
  colony_type:               string
end

enddeffunc

//------------------------------------------------------------------------
// AI_SetColonyTypes
//------------------------------------------------------------------------
function AI_SetColonyTypes returns boolean
params
vars
begin

  // Are we using the Colony Types minister?
  if Sys_Using_AI_Minister(sys_long_Player_ID, "Colony Types") then
    call Set_Colony_Types()
  endif

end

//------------------------------------------------------------------------
// Set_Colony_Types
//------------------------------------------------------------------------
function Set_Colony_Types returns boolean
params
vars
  colony_list:               longlist
  colony_id:                 long
  colony_type:               string
  index:                     long
  total_colonies:            long
begin

  // Make a list of our colonies
  call Sys_Get_List_Of_Players_Colonies(sys_long_Player_ID, colony_list)

  // Choose a colony type for colonies that don't have one or to change it
  set total_colonies := colony_list.count()
  if (total_colonies > 0) then
    // Debug output
    call Sys_Debug_Print("Colonies", "-------------")
    call Sys_Debug_Print("Colonies", "Colony Types:")

    for index := 1 to total_colonies do
      set colony_id := colony_list.get(index)
      set colony_type := Sys_Get_Planet_Colony_Type(colony_id)
      // Assign a Colony Type for colonies without a type
      if (colony_type = "") then
        set colony_type := Pick_Colony_Type(colony_id, total_colonies)
        call Sys_Set_Planet_Colony_Type(colony_id, colony_type)
      else
        // Update Colony Type if current Colony Type is invalid
        set colony_Type := Update_Colony_Type(colony_id, colony_type)
      endif
    endfor
  endif

end

//------------------------------------------------------------------------
// Pick_Colony_Type
//------------------------------------------------------------------------
function Pick_Colony_Type returns string
params  
  colony_id:                 long
  total_colonies:            long
vars
  new_colony_type:           string := ""
  sys_id:                    long
  planet_name:               string
  planet_size:               string
  planet_breathable:         boolean
  planet_facility_space:     long
  planet_value:              resources
  planet_minerals:           real
  planet_organics:           real
  planet_radioactives:       real
  num_colonies:              long
  pct_colonies:              long
  mining_min_value:          long := 100
  farming_min_value:         long := 120
  refining_min_value:        long := 120
  research_colony_pct:       long := 20
  intel_colony_pct:          long := 10
  loop_count:                long := 0
  skip_research_intel:       boolean := FALSE
  skip_military:             boolean := FALSE
  skip_resupply:             boolean := FALSE
  skip_storage:              boolean := FALSE
  debug_str:                 string
begin

  // Get our planet's details
  set sys_id := Sys_Get_Space_Object_System_Location(colony_id)
  set planet_name := Sys_Get_Space_Object_Name(colony_id)
  set planet_size := Sys_Get_Planet_Size(colony_id)
  set planet_breathable := Sys_Is_Planet_Breathable(colony_id, sys_long_Player_ID)
  set planet_facility_space := Sys_Get_Space_Object_Facility_Total_Space(colony_id)
  set planet_value := Sys_Get_Space_Object_Planet_Value(colony_id)
  set planet_minerals := planet_value.get(RESOURCE_TYPE_MINERALS)
  set planet_organics := planet_value.get(RESOURCE_TYPE_ORGANICS)
  set planet_radioactives := planet_value.get(RESOURCE_TYPE_RADIOACTIVES)

  // Special conditions

  // Adjust colony type modifiers for planet size and breathability
  case planet_size
    "Tiny":
      if (planet_breathable) then
        // No modifiers
      else
        set mining_min_value := mining_min_value + 15
        set farming_min_value := farming_min_value + 10
        set refining_min_value := refining_min_value + 10
        set research_colony_pct := research_colony_pct - 20
        set intel_colony_pct := intel_colony_pct - 10
      endif
    "Small":
      if (planet_breathable) then
        // No Modifiers
      else
        set mining_min_value := mining_min_value + 10
        set farming_min_value := farming_min_value + 5
        set refining_min_value := refining_min_value + 5
        set research_colony_pct := research_colony_pct - 15
      endif
    "Medium":
      if (planet_breathable) then
        set mining_min_value := mining_min_value - 5
        set research_colony_pct := research_colony_pct + 10
      else
        set mining_min_value := mining_min_value + 5
        set research_colony_pct := research_colony_pct - 10
        set intel_colony_pct := intel_colony_pct + 5
      endif
    "Large":
      if (planet_breathable) then
        set mining_min_value := mining_min_value - 5
        set farming_min_value := farming_min_value + 5
        set refining_min_value := refining_min_value + 5
        set research_colony_pct := research_colony_pct + 15
      else
        set mining_min_value := mining_min_value + 5
        set farming_min_value := farming_min_value - 5
        set refining_min_value := refining_min_value - 5
        set research_colony_pct := research_colony_pct - 5
        set intel_colony_pct := intel_colony_pct + 5
      endif
    "Huge":
      if (planet_breathable) then
        set mining_min_value := mining_min_value - 10
        set farming_min_value := farming_min_value + 5
        set refining_min_value := refining_min_value + 5
        set research_colony_pct := research_colony_pct + 20
      else
        set mining_min_value := mining_min_value + 5
        set farming_min_value := farming_min_value - 10
        set refining_min_value := refining_min_value - 10
        set intel_colony_pct := intel_colony_pct + 10
      endif
  endcase

  // Adjust colony modifiers for our resource availability
  // Minerals
  if (bool_Race_Minerals_Low) then
    set mining_min_value := mining_min_value - 20
  endif
  if (bool_Race_Minerals_Prod_Low) then
    set mining_min_value := mining_min_value - 10
  endif
  if (bool_Race_Minerals_High) then
    set mining_min_value := mining_min_value + 20
  endif
  if (bool_Race_Minerals_Prod_High) then
    set mining_min_value := mining_min_value + 10
  endif
  // Organics
  if (bool_Race_Organics_Low) then
    set farming_min_value := farming_min_value - 10
  endif
  if (bool_Race_Organics_Prod_Low) then
    set farming_min_value := farming_min_value - 10
  endif
  if (bool_Race_Organics_High) then
    set farming_min_value := farming_min_value + 15
  endif
  if (bool_Race_Organics_Prod_High) then
    set farming_min_value := farming_min_value + 10
  endif
  // Radioactives
  if (bool_Race_Radioactives_Low) then
    set refining_min_value := refining_min_value - 20
  endif
  if (bool_Race_Radioactives_Prod_Low) then
    set refining_min_value := refining_min_value - 10
  endif
  if (bool_Race_Radioactives_High) then
    set refining_min_value := refining_min_value + 15
  endif
  if (bool_Race_Radioactives_Prod_High) then
    set refining_min_value := refining_min_value + 10
  endif

  // Adjust our colony modifiers for any racial technologies
  if (bool_Race_Uses_Organic_Tech) then
    set farming_min_value := farming_min_value - 15
  endif
  if (bool_Race_Uses_Crystalline_Tech) or (bool_Race_Uses_Interdimensional_Tech) or (bool_Race_Uses_Temporal_Tech) then
    set refining_min_value := refining_min_value - 15
  endif

  // Reduce resource colony modifiers further if we are at max tech or have lots of intel
  if (bool_Race_At_Max_Tech) or (bool_Race_At_Max_Intel) then
    set mining_min_value := mining_min_value - 10
    set farming_min_value := farming_min_value - 20
    set refining_min_value := refining_min_value - 20
  endif

  // Convert resource value for finite resource games
  if Sys_Is_Finite_Resource_Game() then
    // Minerals
    if (planet_minerals <= 100000) then
      set planet_minerals := 0.0
    else
      set planet_minerals := (planet_minerals / 80000) + 25
    endif
    // Organics
    if (planet_organics <= 100000) then
      set planet_organics := 0.0
    else
      set planet_organics := (planet_organics / 80000) + 25
    endif
    // Radioactives
    if (planet_radioactives <= 100000) then
      set planet_radioactives := 0.0
    else
      set planet_radioactives := (planet_radioactives / 80000) + 25
    endif
    // Adjust our minimum requirements for resource value
    set mining_min_value := 100
    set farming_min_value := 100
    set refining_min_value := 100
  endif

  // Adjust our colony modifiers for non-resource colonies if we have lots of resources
  if (bool_Race_Minerals_High) and (bool_Race_Organics_High) and (bool_Race_Radioactives_High) then
    if (not bool_Race_At_Max_Tech) then
      set research_colony_pct := research_colony_pct + 5
    endif
    if (not bool_Race_At_Max_Intel) and (bool_Intel_Is_Allowed) then
      set intel_colony_pct := intel_colony_pct + 5
    endif
  endif

  // Adjust our colony modifiers based on our Empire category
  case lng_AI_Categorization
    AI_CATEGORY_PEACEFUL:
      set research_colony_pct := research_colony_pct + 5
      set intel_colony_pct := intel_colony_pct - 5
    AI_CATEGORY_NEUTRAL:
      // No Changes
    AI_CATEGORY_AGGRESSIVE:
      set research_colony_pct := research_colony_pct - 5
      set intel_colony_pct := intel_colony_pct + 5
    AI_CATEGORY_XENOPHOBIC:
      set research_colony_pct := research_colony_pct - 5
      set intel_colony_pct := intel_colony_pct + 5
  endcase

  // Adjust research/intel colony modifiers based on current game conditions
  if (bool_Race_At_Max_Tech) then
    set research_colony_pct := 0
  else
    if (bool_Enemy_Far_Ahead_In_Research) then
      set research_colony_pct := research_colony_pct + 5
    endif
    if (bool_Race_Boost_Research) then
      set research_colony_pct := research_colony_pct + 5
    endif
  endif
  if (bool_Intel_Is_Allowed) then
    if (bool_Race_At_Max_Intel) then
      set intel_colony_pct := 5
    else
      if (bool_Enemy_Intel_Overload) or (bool_Enemy_Far_Ahead_In_Intel) then
        set intel_colony_pct := intel_colony_pct + 5
      endif
      if (bool_Race_Boost_Intel) then
        set intel_colony_pct := intel_colony_pct + 5
      endif
    endif
  else
    set intel_colony_pct := 0
  endif

  // Debug output
  call Sys_Debug_Print("Colonies", "  - " + planet_name + " values:")
  set debug_str := Sys_Convert_Long_To_String(Sys_Trunc(planet_minerals)) + "% Minerals, " + Sys_Convert_Long_To_String(Sys_Trunc(planet_organics)) + "% Organics, " + Sys_Convert_Long_To_String(Sys_Trunc(planet_radioactives)) + "% Radioactives"
  call Sys_Debug_Print("Colonies", "    - Planet % value = " + debug_str)

  call Sys_Debug_Print("Colonies", "    - Mining Colony % Min = " + Sys_Convert_Long_To_String(mining_min_value) + "%")
  call Sys_Debug_Print("Colonies", "    - Farming Colony %  Min = " + Sys_Convert_Long_To_String(farming_min_value) + "%")
  call Sys_Debug_Print("Colonies", "    - Refining Colony % Min = " + Sys_Convert_Long_To_String(refining_min_value) + "%")
  call Sys_Debug_Print("Colonies", "    - Research Colony % = " + Sys_Convert_Long_To_String(research_colony_pct) + "%")
  call Sys_Debug_Print("Colonies", "    - Intel Colony % = " + Sys_Convert_Long_To_String(intel_colony_pct) + "%")

  // Pick a colony type for our new colony
  // Resupply Base then Construction Yard in systems with limited planets (ie asteroid belt systems)
  if (lst_AI_Systems_With_Limited_Planets.indexof(sys_id) > 0) then
    if (not Sys_Player_Has_Colony_In_System(sys_long_Player_ID, sys_id)) then
      set new_colony_type := AI_STR_COLONY_TYPE_RESUPPLY
    else
      set new_colony_type := AI_STR_COLONY_TYPE_CONSTRUCTION
    endif
    // Debug output
    call Sys_Debug_Print("Colonies", "    - Limited planet system (RD or SY)")
  endif

  // Mining colony?
  if (new_colony_type = "") then
    if (planet_minerals >= mining_min_value) then
      set new_colony_type := AI_STR_COLONY_TYPE_MINING
    endif
  endif
  // Resource colony?
  if (new_colony_type = "") then
    if (planet_minerals >= mining_min_value) and (planet_organics >= farming_min_value) and (planet_radioactives >= refining_min_value) then
      set new_colony_type := AI_STR_COLONY_TYPE_RESOURCE
    endif
    // If at max tech, consider planets with generally good resource value
    if (bool_Race_At_Max_Tech) and ((planet_minerals + planet_organics + planet_radioactives) > 300) then
      set new_colony_type := AI_STR_COLONY_TYPE_RESOURCE
    endif
  endif
  // Mining & Refining colony?
  if (new_colony_type = "") then
    if (planet_minerals >= mining_min_value) and (planet_radioactives >= refining_min_value) then
      set new_colony_type := AI_STR_COLONY_TYPE_MINING_REFINING
    endif
  endif
  // Mining & Farming colony?
  if (new_colony_type = "") then
    if (planet_minerals >= mining_min_value) and (planet_organics >= farming_min_value) then
      set new_colony_type := AI_STR_COLONY_TYPE_MINING_FARMING
    endif
  endif
  // Farming colony?
  if (new_colony_type = "") then
    if (planet_organics >= farming_min_value) then
      set new_colony_type := AI_STR_COLONY_TYPE_FARMING
    endif
  endif
  // Farming & Refining colony?
  if (new_colony_type = "") then
    if (planet_organics >= farming_min_value) and (planet_radioactives >= refining_min_value) then
      set new_colony_type := AI_STR_COLONY_TYPE_FARMING_REFINING
    endif
  endif
  // Refining colony?
  if (new_colony_type = "") then
    if (planet_radioactives >= refining_min_value) then
      set new_colony_type := AI_STR_COLONY_TYPE_REFINING
    endif
  endif

  // If our colony is not good for resources, choose another type:
  // Approximately 20% of our colonies should be Research Compounds
  if (new_colony_type = "") and (research_colony_pct > 0) then
    set num_colonies := Sys_Get_Number_Of_Colony_Types_In_Empire(sys_long_Player_ID, AI_STR_COLONY_TYPE_RESEARCH)
    set pct_colonies := Sys_Trunc(num_colonies / total_colonies * 100)
    if (research_colony_pct > pct_colonies) then
      set new_colony_type := AI_STR_COLONY_TYPE_RESEARCH
    endif
  endif

  // Approximately 10% of our colonies should be Intel Compounds
  if (new_colony_type = "") and (intel_colony_pct > 0) then
    set num_colonies := Sys_Get_Number_Of_Colony_Types_In_Empire(sys_long_Player_ID, AI_STR_COLONY_TYPE_INTEL)
    set pct_colonies := Sys_Trunc(num_colonies / total_colonies * 100)
    if (intel_colony_pct > pct_colonies) then
      set new_colony_type := AI_STR_COLONY_TYPE_INTEL
    endif
  endif

  // Approximately 10% of our colonies should be minor colony types
  // Minor colony type overrides
  set skip_research_intel := bool_Race_At_Max_Tech and (not bool_Intel_Is_Allowed)
  set skip_military := Is_Colony_Type_In_System(sys_id, AI_STR_COLONY_TYPE_MILITARY) and (bool_Race_Is_Not_Alone) and (planet_facility_space < 3000 and planet_facility_space > 5000)
  set skip_resupply := Is_Colony_Type_In_System(sys_id, AI_STR_COLONY_TYPE_RESUPPLY) and ((Sys_Get_Facility_Count_In_System(sys_long_Player_ID, sys_id, lng_facility_id_resupply_depot) + (Sys_Get_System_Queues_Specific_Facility_Count(sys_long_Player_ID, sys_id, "Resupply Depot"))) < 2)
  set skip_storage := (not bool_Race_Needs_Minerals_Storage) and (not bool_Race_Needs_Organics_Storage) and (not bool_Race_Needs_Radioactives_Storage) and (planet_facility_space > 15000)

  if (new_colony_type = "") then
    loop
      set loop_count := loop_count + 1
      case Sys_Get_Random_Long(1, 4)
        1:
          // Storage Compound
          if (not skip_storage) then
            set new_colony_type := AI_STR_COLONY_TYPE_STORAGE
          endif
        2:
          // Resupply Base
          if (not skip_resupply) then
            set new_colony_type := AI_STR_COLONY_TYPE_RESUPPLY
          endif
        3:
          // Military Installation
          if (not skip_military) then
            set new_colony_type := AI_STR_COLONY_TYPE_MILITARY
          endif
        4:
          // Research & Intel Compound or Construction Yard (Default)
          if (not skip_research_intel) then
            set new_colony_type := AI_STR_COLONY_TYPE_RESEARCH_INTEL
          else
            set new_colony_type := AI_STR_COLONY_TYPE_CONSTRUCTION
          endif
      endcase
      exitwhen (loop_count >= 20) or (new_colony_type <> "")
    endloop
  endif

  // Debug output
  call Sys_Debug_Print("Colonies", "  - Colony Type = " + new_colony_type)

  return new_colony_type
end

//------------------------------------------------------------------------
// Update_Colony_Type
//------------------------------------------------------------------------
function Update_Colony_Type returns string
params  
  colony_id:                 long
  colony_type:               string
vars
  new_colony_type:           string := ""
  colony_name:               string
  planet_value:              resources
  planet_minerals:           real
  planet_organics:           real
  planet_radioactives:       real
  num_facilities:            long
  num_spaceports:            long
  num_spaceyards:            long
  num_resupply_depots:       long
  num_minerals:              long
  num_organics:              long
  num_radioactives:          long
  num_research:              long
  num_monolith:              long
  num_intel:                 long
  good_minerals:             boolean := FALSE
  good_organics:             boolean := FALSE
  good_radioactives:         boolean := FALSE
begin

  // Only consider changing the colony's colony type if at maximum number of facilities
  if (Sys_Get_Space_Object_Facility_Space_Remaining(colony_id) = 0) then
    // Get our colony's name
    set colony_name := Sys_Get_Space_Object_Name(colony_id)
    // Get our colony's resource value
    set planet_value := Sys_Get_Space_Object_Planet_Value(colony_id)
    set planet_minerals := planet_value.get(RESOURCE_TYPE_MINERALS)
    set planet_organics := planet_value.get(RESOURCE_TYPE_ORGANICS)
    set planet_radioactives := planet_value.get(RESOURCE_TYPE_RADIOACTIVES)

    // Convert resource value for finite resources game
    if Sys_Is_Finite_Resource_Game() then
      // Minerals
      if (planet_minerals <= 100000) then
        set planet_minerals := 0.0
      else
        set planet_minerals := (planet_minerals / 80000) + 25
      endif
      // Organics
      if (planet_organics <= 100000) then
        set planet_organics := 0.0
      else
        set planet_organics := (planet_organics / 80000) + 25
      endif
      // Radioactives
      if (planet_radioactives <= 100000) then
        set planet_radioactives := 0.0
      else
        set planet_radioactives := (planet_radioactives / 80000) + 25
      endif
    endif

    // Note the quality of our planet's resources
    set good_minerals := planet_minerals >= 100
    set good_organics := planet_organics >= 100
    set good_radioactives := planet_radioactives >= 100

    // Count the number of basic facilities present
    set num_facilities := Get_Total_Number_Of_Facilities_On_Planet(colony_id)
    set num_spaceports := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_spaceport)
    set num_spaceyards := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_space_yard)
    set num_resupply_depots := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_resupply_depot)
    set num_minerals := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_minerals)
    set num_organics := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_organics)
    set num_radioactives := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_radioactives)
    set num_monolith := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_monolith)
    set num_research := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_research_pts)
    set num_intel := Get_Number_Of_Facilities_On_Planet(colony_id, lng_facility_id_intel_pts)

    // Military Installations should have a Space Yard and Resupply Depot
    if (colony_type = AI_STR_COLONY_TYPE_MILITARY) then
      if (num_spaceyards = 0) or (num_resupply_depots = 0) then
        set new_colony_type := "Change!"
      endif
    endif

    // Construction Yards should have a Space Yard
    if (colony_type = AI_STR_COLONY_TYPE_CONSTRUCTION) or (new_colony_type = "Change!") then
      if (num_spaceyards = 0) then
        set new_colony_type := "Change!"
      else
        if (num_facilities = 1) then
          set new_colony_type := AI_STR_COLONY_TYPE_CONSTRUCTION
        endif
      endif
    endif

    // Resupply Bases should have a Resupply Depot
    if (colony_type = AI_STR_COLONY_TYPE_RESUPPLY) or (colony_type = "Change!") then
      if (num_resupply_depots = 0) then
        set new_colony_type := "Change!"
      else
        if (num_facilities = 1) then
          set new_colony_type := AI_STR_COLONY_TYPE_RESUPPLY
        endif
      endif
    endif

    // Resource Colonies
    if (colony_type = AI_STR_COLONY_TYPE_MINING) or (colony_type = AI_STR_COLONY_TYPE_FARMING) or (colony_type = AI_STR_COLONY_TYPE_REFINING) or (colony_type = AI_STR_COLONY_TYPE_MINING_FARMING) or (colony_type = AI_STR_COLONY_TYPE_MINING_REFINING) or (colony_type = AI_STR_COLONY_TYPE_FARMING_REFINING) or (colony_type = AI_STR_COLONY_TYPE_RESOURCE) or (new_colony_type = "Change!") then
      // Resource Colony
      if (num_monolith > 0) or ((num_minerals > 0) and (num_organics > 0) and (num_radioactives > 0)) then
        set new_colony_type := AI_STR_COLONY_TYPE_RESOURCE
      endif
      // Mining & Farming Colony
      if (num_minerals > 0) and (num_organics > 0) and (num_radioactives = 0) then
        set new_colony_type := AI_STR_COLONY_TYPE_MINING_FARMING
      endif
      // Mining & Refining Colony
      if (num_minerals > 0) and (num_organics = 0) and (num_radioactives > 0) then
        set new_colony_type := AI_STR_COLONY_TYPE_MINING_REFINING
      endif
      // Farming & Refining Colony
      if (num_minerals = 0) and (num_organics > 0) and (num_radioactives > 0) then
        set new_colony_type := AI_STR_COLONY_TYPE_FARMING_REFINING
      endif
    endif

    // Research or Intelligence Compounds
    if (colony_type = AI_STR_COLONY_TYPE_RESEARCH) or (colony_type = AI_STR_COLONY_TYPE_INTEL) or (colony_type = AI_STR_COLONY_TYPE_RESEARCH_INTEL) or (new_colony_type = "Change!") then
      // Research & Intel Compound
      if (num_research > 0) and (num_intel > 0) then
        if (bool_Race_At_Max_Tech) then
          if (good_minerals or good_organics or good_radioactives) then
            set new_colony_type := AI_STR_COLONY_TYPE_RESOURCE
          else
            set new_colony_type := AI_STR_COLONY_TYPE_INTEL
          endif
        endif
      endif
      // Research Compound
      if (num_research > 0) and (num_intel > 0) then
        // If at max tech and the planet is valuable, consider switching to resource colony
        if (bool_Race_At_Max_Tech) then
          if (good_minerals or good_organics or good_radioactives) then
            set new_colony_type := AI_STR_COLONY_TYPE_RESOURCE
          else
            set new_colony_type := AI_STR_COLONY_TYPE_RESEARCH_INTEL
          endif
        else
          set new_colony_type := AI_STR_COLONY_TYPE_RESEARCH
        endif
      endif
      // Intelligence Compound
      if (num_research = 0) and (num_intel > 0) then
        if (bool_Race_At_Max_Tech) and (good_minerals or good_organics or good_radioactives) and ((bool_Race_At_Max_Intel and (num_intel < 10)) or (num_intel < 5)) then
          set new_colony_type := AI_STR_COLONY_TYPE_RESOURCE
        else
          set new_colony_type := AI_STR_COLONY_TYPE_INTEL
        endif
      endif
    endif

    // Special cases for single facility colonies
    if (num_facilities = 1) then
      if (num_spaceports = 1) then
        set new_colony_type := AI_STR_COLONY_TYPE_RESOURCE
      endif
      if (num_spaceyards = 1) then
        set new_colony_type := AI_STR_COLONY_TYPE_CONSTRUCTION
      endif
      if (num_resupply_depots = 1) then
        set new_colony_type := AI_STR_COLONY_TYPE_RESUPPLY
      endif
    endif
  endif

  // Change the colony type if it needs to be updated
  if (colony_type <> new_colony_type) and (new_colony_type <> "") and (new_colony_type <> "Change!") then
    // Change the Colony type
    call Sys_Set_Planet_Colony_Type(colony_id, new_colony_type)
    // Debug output
    call Sys_Debug_Print("Colonies", "  - " + colony_name)
    call Sys_Debug_Print("Colonies", "    - " + colony_type + " re-designated " + new_colony_type)
  endif

  return new_colony_type
end

//------------------------------------------------------------------------